package cc.rengu.redp.hawk.controller;


import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.domain.RedpResponse;
import cc.rengu.redp.hawk.domain.HawkMonitorConfigVo;
import cc.rengu.redp.hawk.service.HawkMonitorConfigService;
import cc.rengu.redp.hawk.service.IndexArithmeticService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xushitang
 */
@Slf4j
@RestController
@RequestMapping("/hawk/config")
public class HawkMonitorConfigController extends BaseController {

    @Autowired
    private HawkMonitorConfigService hawkMonitorConfigService;
    @Autowired
    private IndexArithmeticService indexArithmeticService;

    @GetMapping
    public Map<String, Object> list(QueryRequest request, HawkMonitorConfigVo hawkMonitorConfigVo) {
        return getDataTable(this.hawkMonitorConfigService.findHawkMonitorConfig(request, hawkMonitorConfigVo));
    }

    @Log("新增监控任务")
    @PostMapping("/add")
    public RedpResponse add(@ApiParam(required = true, value = "json格式字符串参数") String hawkMonitorConfigString) {
        HawkMonitorConfigVo hawkMonitorConfigVo = JSON.parseObject(hawkMonitorConfigString, HawkMonitorConfigVo.class);
        try {
            this.hawkMonitorConfigService.createHawkMonitorConfig(hawkMonitorConfigVo);
        } catch (Exception e) {
            String message = "新增监控任务失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("新增监控任务成功");
    }

    @Log("删除监控任务")
    @DeleteMapping("/delete")
    public RedpResponse delete(String[] ids) {
        try {
            this.hawkMonitorConfigService.deleteHawkMonitorConfig(ids);
        } catch (Exception e) {
            String message = "删除监控任务失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("删除监控任务成功");
    }

    @Log("修改监控任务")
    @PutMapping("/update")
    public RedpResponse update(String hawkMonitorConfigString) {
        HawkMonitorConfigVo hawkMonitorConfigVo = JSON.parseObject(hawkMonitorConfigString, HawkMonitorConfigVo.class);
        try {
            this.hawkMonitorConfigService.updateHawkMonitorConfig(hawkMonitorConfigVo);
        } catch (Exception e) {
            String message = "修改监控任务失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        return RedpResponse.getInstance().success().message("修改监控任务成功");
    }

    @Log("启停监控任务")
    @PutMapping("/useEnable")
    public RedpResponse useEnable(String id, Boolean useEnable) {
        try {
            this.hawkMonitorConfigService.useEnableHawkMonitorConfig(id, useEnable);
        } catch (Exception e) {
            String message = "启停监控任务失败";
            log.error(message, e);
            return RedpResponse.getInstance().failure().message(message);
        }

        if (useEnable) {
            return RedpResponse.getInstance().success().message("开启监控任务成功");
        } else {
            return RedpResponse.getInstance().success().message("关闭监控任务成功");
        }
    }

    /**
     * 获取监控分组
     *
     * @return
     */
    @GetMapping("/groupList")
    public List<String> groupList() {
        return this.hawkMonitorConfigService.groupList();
    }

    /**
     * 获取指标算法列表
     *
     * @return
     */
    @GetMapping("/algorithmList")
    public Map<String, String> algorithmList() {
        return indexArithmeticService.getAlgorithmList()
                .entrySet().stream()
                .collect(Collectors.toMap(
                        i -> i.getKey(),
                        i -> i.getValue().algorithmChineseName()));
    }

    /**
     * 获取指定指标算法的参数示例
     *
     * @param algorithm
     * @return
     */
    @GetMapping("/algorithmParamsSample")
    public String algorithmParamsSample(String algorithm) {
        return indexArithmeticService.getAlgorithmParamsSample(algorithm);
    }
}